package com.lin.service;

import com.lin.dao.UserMapper;
import com.lin.pojo.User;
import com.lin.util.CreateDate;
import com.lin.util.DealJson;
import com.lin.util.RandomID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description 用户的逻辑处理
 * @Author LinZePeng
 * @Date 2021/5/3 14:16
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;

	/**
	 * @Description 功能实现（查询所有用户）
	 * @Param []
	 * @return java.util.List<com.lin.pojo.User>
	 */
	@Override
	public List<User> queryUser() {
		return userMapper.queryUser();
	}


	/**
	 * @Description 功能实现（将前端传回的JSON进行封装）
	 * @Param [user]
	 * @return com.lin.pojo.User
	 */
	@Override
	public void  toAddUser(String user) {
		/*
		  @Description 管理员验证码
		 */
		final String CODE = "4399250";

		/*
		  @Description 用户身份证明
		 */
		String identity ;

		User userTo = (User) DealJson.getObject(user, User.class);
		assert userTo != null;
		String num = RandomID.getRandomID();
		Boolean aBoolean = false;
		if (userMapper.queryByID(num) != null) {
			aBoolean=true;
		}
		while (aBoolean){
			num = RandomID.getRandomID();
			if (userMapper.queryByID(num) == null) {
				aBoolean=false;
			}
		};
		userTo.setUserID(num);
		userTo.setUserTime(CreateDate.getCreateDate());
		userTo.setUserRank(Integer.valueOf("0"));
		userTo.setUserStatus(Integer.valueOf("1"));

		if(userTo.getAdmID().equals(CODE)){
			identity = "管理员" ;
		}
		else {
			identity = "普通用户";
		}
		userTo.setAdmID(identity);

		this.addUser(userTo);
	}

	/**
	 * @Description 功能实现(增加用户)
	 * @Param [user]
	 * @return java.lang.Boolean
	 */
	@Override
	public Boolean addUser(User user) {
		return userMapper.addUser(user);
	}

	/**
	 * @Description 功能实现(按名字查询用户)
	 * @Param [userName]
	 * @return com.lin.pojo.User
	 */
	@Override
	public User queryByName(String userName) { return userMapper.queryByName(userName); }

	/**
	 * @Description 功能实现(按ID查询用户)
	 * @Param [userId]
	 * @return java.lang.Boolean
	 */
	@Override
	public Boolean queryByID(String userId) {
		return userMapper.queryByID(userId);
	}

	/**
	 * @Description 功能实现（验证登入信息是否正确）
	 * @Param [name, phone, email]
	 * @return java.lang.Boolean
	 */
	@Override
	public Boolean queryCheck(String name, String phone, String email) {
		return userMapper.queryCheck(name,phone,email) != null;
	}

}
